package com.example.myretrofit2.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myretrofit2.Car;
import com.example.myretrofit2.R;
import com.example.myretrofit2.common.ImageLoaderManager;
import com.example.myretrofit2.common.MyApplication;
import com.example.myretrofit2.fragment.CarFragment;
import com.example.myretrofit2.presenter.CarPresenter;
import com.example.myretrofit2.presenter.MySubscriber;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class CarAdapter extends RecyclerView.Adapter<CarAdapter.MyHolder>  {
Context context;
List<Car> carList;
//Car car;
int count;
CarFragment carFragment;
float total;
TextView textView;
    public CarAdapter(Context context, List<Car> carList,float total,TextView textView) {
        this.context = context;
        this.carList = carList;
        this.total = total;
        this.textView = textView;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.car,null);
        MyHolder myHolder = new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder holder, final int position) {
        final Car car = carList.get(position);
        holder.goodsPrice.setText(carList.get(position).getPrice()+"");
        holder.goodsName.setText(carList.get(position).getGoodName());
        holder.num.setText(carList.get(position).getNum()+"");
        holder.itemView.setTag(carList.get(position));
        ImageLoader.getInstance().displayImage(MyApplication.imageUrl+carList.get(position).getImg(),holder.imageView);
        carFragment=new CarFragment();
        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                count = car.getNum();
                holder.num.setText(++count +"");
                car.setNum(count);
                CarPresenter.updateCar(new MySubscriber<Boolean>(MyApplication.getContext()) {
                    @Override
                    public void onNext(Boolean aBoolean) {
                        total = total+car.getPrice();
                        carFragment.sum(total,textView);
//                        notifyItemChanged(position);
//                        notifyItemRangeChanged(position,getItemCount()-position);
                    }
                },car);
            }
        });
        holder.sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                count = car.getNum();
                if (count > 1) {
                    holder.num.setText(--count + "");
                    car.setNum(count);
                    CarPresenter.updateCar(new MySubscriber<Boolean>(MyApplication.getContext()) {
                        @Override
                        public void onNext(Boolean aBoolean) {
                            total = total-car.getPrice();
                            carFragment.sum(total,textView);
                        }
                    }, car);
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return carList.size();
    }




    public class MyHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView goodsName;
        TextView goodsPrice;
        TextView num;
        Button add;
        Button sub;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.goods_img_dw);
            goodsName = itemView.findViewById(R.id.goods_name_dw);
            goodsPrice = itemView.findViewById(R.id.goods_price_dw);
            num = itemView.findViewById(R.id.num);
            add = itemView.findViewById(R.id.add);
            sub = itemView.findViewById(R.id.sub);
        }
    }
}
